var jsonFile = require('jsonfile')
var fileName = 'guangtou.json'
const axios = require('axios')
var jsonData = jsonFile.readFileSync(fileName)
const qs = require('qs')

const phone = jsonData.phone
const pwd = jsonData.pwd
const token = jsonData.token
const pay_pwd = jsonData.pay_pwd
const goods = jsonData.goods
var requestArray = []

const instance = axios.create({
  credentials: true,
  baseURL: 'https://nft.xhwc.net',
  headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' }
})
instance.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;'
instance.defaults.headers['token'] = token





pushList()


// 商品列表填充
function pushList() {
  setInterval(() => {
    for (let item in goods) {
      instance.get('/api/trade/tradeGoodsList?page=1&pagesize=10&search=&time_order=&price_order=1&goods_id=' + goods[item].id).then(res => {
        let data = res.data.data.data[0]
        let price = data.price
        let id = data.users_goods_id
        let name = data.name
        console.log('商品:' + name + '最低价为:' + price + '------价格低于:' + goods[item].price + '时将自动锁定')
        if (price <= goods[item].price) {
          instance.get('/api/orders/tradeGoods?users_goods_id=' + id).then(res => {
            if (res.data.code == 1) {
              console.log(res.data.msg)
              axios.post('https://open.feishu.cn/open-apis/bot/v2/hook/9234b14f-e535-41e2-98dd-309f1fd2b580', { "msg_type": "text", "content": { "text": "光头锁单成功,请上号支付!!!" + ",价格为:" + price } })
            } else {
              console.log('商品被抢了 or 其他')
            }
          }).catch(err => {
            console.log('频繁')
          })
        }
      }).catch(err => {
        console.log('请求频繁')
      })
    }
  }, 400)
}